package com.ityy.ruiji.controller;

import com.ityy.ruiji.common.R;
import com.ityy.ruiji.entity.dto.DishDto;
import com.ityy.ruiji.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @GetMapping("/page")
    public R page(@RequestParam(defaultValue = "1") Integer page ,
                  @RequestParam(defaultValue = "10") Integer pageSize ,
                     String name){
        R result = dishService.findByPage(page,pageSize,name);
        return result;
    }

    @PostMapping
    public R save(@RequestBody DishDto dishDto , HttpSession httpSession){
        Long employee = (Long) httpSession.getAttribute("employee");
        dishDto.setCreateUser(employee);
        dishDto.setUpdateUser(employee);
        R save = dishService.save(dishDto);
        return save;
    }

    @GetMapping ("/{id}")
    public R findById(@PathVariable Long id){
       R result = dishService.findById(id);
       return result;
    }

    @PutMapping
    public R updateById(@RequestBody DishDto dishDto , HttpSession session){
        Long employee = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(employee);
        R result = dishService.updateById(dishDto);
       return result;
    }

    @GetMapping("/list")
    public R list(Long categoryId,Integer status){
        R result = dishService.findId(categoryId,status);
        return result;
    }
}
